const app = getApp()

Page({

  options: {
    addGlobalClass: true,
  },
  data: {
    is_admin: false,
    starCount: 0,
    forksCount: 0,
    visitTotal: 0,

    visitTotal_num: 19119,
    starCount_num: 0,
    forksCount_num: 0,
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     if (wx.getStorageSync("configs").admin_openid_list.indexOf(app.globalData.openid) !=-1) {
      this.setData({
        is_admin: true
      })
    } else {
      this.setData({
        is_admin: wx.getStorageSync("is_admin")
      })
    }
    if (wx.getStorageSync("configs")){
      var configs = wx.getStorageSync("configs")
    this.setData({
      visitTotal_num: configs.visitTotal_num,
      starCount_num: configs.starCount_num,
      forksCount_num: configs.forksCount_num,
    })
  }


    console.log("success")
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })
    let i = 0;
    numDH();
    function numDH() {
      if (i < 20) {
        setTimeout(function () {
          that.setData({
            starCount: i,
            forksCount: i,
            visitTotal: i
          })
          i++
          numDH();
        }, 20)
      } else {
        that.setData({
          starCount: that.coutNum(that.data.starCount_num),
          forksCount: that.coutNum(that.data.forksCount_num),
          visitTotal: that.coutNum(that.data.visitTotal_num)
        })
      }
    }
    wx.hideLoading()




  },
  coutNum(e) {
    if (e > 1000 && e < 10000) {
      e = (e / 1000).toFixed(1) + 'k'
    }
    if (e > 10000) {
      e = (e / 10000).toFixed(1) + 'W'
    }
    return e
  },
  show_yx: function (res) {
    wx.setClipboardData({
      data: res.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '延旭未来，改变既是现在！微信号已复制:18580015335',
          icon: "none",
          duration: 1000,
        })
      }
    })
  },


  


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  // liftimes:{

  // },
  onLoad() {
    // console.log(wx.getStorageSync("configs").admin_openid_list.indexOf("12333"))
    // console.log(wx.getStorageSync("configs").admin_openid_list.indexOf("ohi2m5ITTPWn8NRlT9g5bi3xzJlE"))
    // console.log(app.globalData.openid)
    // console.log(wx.getStorageSync("configs").admin_openid_list.indexOf(app.globalData.openid))

    // if (wx.getStorageSync("configs").admin_openid_list){

    // }




    if (wx.getStorageSync("configs").admin_openid_list.indexOf(app.globalData.openid)!=-1) {
      this.setData({
        is_admin: true
      })
    } else {
      this.setData({
        is_admin: wx.getStorageSync("is_admin")
      })
    }
    console.log("success1")
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    })
    
   
  
    wx.hideLoading()
  },
  clearStorage() {
    wx.showModal({
      title: '注意',
      content: "重置将会丢失已经储\r\n存的数据，是否继续？",
      showCancel: true, //是否显示取消按钮
      cancelText: "算了", //默认是“取消”
      cancelColor: '#b9b9b9', //取消文字的颜色
      confirmText: "继续", //默认是“确定”
      confirmColor: '#000000', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          console.log("用户取消选择！")
        } else {
          //点击确定
          wx.showLoading({
            title: '重置中...',
          })
          wx.clearStorage()
         
          setTimeout(function () {
            wx.hideLoading()
            wx.reLaunch({
              url: '/pages/index/index',
            })
 
          }

            , 1000)
        }
      },
      fail: function (res) { }, //接口调用失败的回调函数
      complete: function (res) { }, //接口调用结束的回调函数（调用成功、失败都会执行）
    })




  },




  navigate_to: function (res) {
    var url = res.currentTarget.dataset.link
    wx.navigateTo({
      url: url,
    })
  },


  coutNum(e) {
    if (e > 1000 && e < 10000) {
      e = (e / 1000).toFixed(1) + 'k'
    }
    if (e > 10000) {
      e = (e / 10000).toFixed(1) + 'W'
    }
    return e
  },
  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '已复制微信号',
          duration: 1000,
        })
      }
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  showQrcode() {
    wx.previewImage({
      urls: ['https://7961-yanxuweilai-01-1258824549.tcb.qcloud.la/%E8%B5%9E%E8%B5%8F%E7%A0%81/%E8%B5%9E%E8%B5%8F%E7%A0%81.jpg?sign=dbae8d817e46516f6f60c026645f0882&t=1562822144'],
      current: 'https://7961-yanxuweilai-01-1258824549.tcb.qcloud.la/%E8%B5%9E%E8%B5%8F%E7%A0%81/%E8%B5%9E%E8%B5%8F%E7%A0%81.jpg?sign=dbae8d817e46516f6f60c026645f0882&t=1562822144' // 当前显示图片的http链接      
    })
  },
  //  获取或添加地址
  getaddress() {
    console.log("获取地址")
    var that = this
    wx.getSetting({
      success(res) {
        console.log("vres.authSetting['scope.address']：", res.authSetting['scope.address'])
        if (res.authSetting['scope.address']) {
          console.log("111")
          wx.chooseAddress({
            success(res) {
              console.log(res.userName)
              console.log(res.postalCode)
              console.log(res.provinceName)
              console.log(res.cityName)
              console.log(res.countyName)
              console.log(res.detailInfo)
              console.log(res.nationalCode)
              console.log(res.telNumber)
              console.log(res)
              that.setData({
                address: res
              })
            }
          })
          // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问

        } else {
          if (res.authSetting['scope.address'] == false) {
            console.log("222")
            wx.openSetting({
              success(res) {
                console.log(res.authSetting)

              }
            })
          } else {
            console.log("eee")
            wx.chooseAddress({
              success(res) {
                console.log(res.userName)
                console.log(res.postalCode)
                console.log(res.provinceName)
                console.log(res.cityName)
                console.log(res.countyName)
                console.log(res.detailInfo)
                console.log(res.nationalCode)
                console.log(res.telNumber)
              }
            })
          }
        }
      }
    })
  },
  // 打电话
  phonecall(e) {
    console.log(e)
    var phone_no = e.currentTarget.dataset.phone_no
    console.log(phone_no)
    wx.makePhoneCall({
      phoneNumber: phone_no,
    })
  },

})